package as3.component.controls
{
	import flash.display.Bitmap;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import as3.component.manager.UIManager;
	
	/**
	 * 翻页面板
	 * @author 周玉龙
	 * 二〇一一年七月十九日 13:09:24
	 */	
	public class FlipComponent extends Sprite
	{
	
		//--------------------------------------------------------------------------
		//
		//  variable
		//
		//--------------------------------------------------------------------------
		/**点击第一页*/
		public static const CLICK_FIRSTPAGE:String = "click_firstpage";
		
		/**点击下一页*/
		public static const CLICK_NEXTPAGE:String = "click_nextpage";
		
		/**点击上一页*/
		public static const CLICK_PREVIOUSPAGE:String = "click_previouspage";
		
		/**点击最后一页*/
		public static const CLICK_LASTPAGE:String = "click_lastpage";
		
		/**第一页*/
		private var firstPage:SimpleButton;
		
		/**最后一页*/
		private var lastPage:SimpleButton;
		
		/**上一页*/
		private var  previousPage:SimpleButton;
		
		/**下一页*/
		private var nextPage:SimpleButton;
		
		/**页数*/
		private var pageLabel:Label;
		
		public function FlipComponent()
		{
			super();
			init();
		}
	
		//--------------------------------------------------------------------------
		//
		//  property
		//
		//--------------------------------------------------------------------------
		private var _currentPage:uint = 1;

		/**当前页*/
		public function get currentPage():uint
		{
			return _currentPage;
		}

		/**
		 * @private
		 */
		public function set currentPage(value:uint):void
		{
			_currentPage = value;
			pageLabel.text = currentPage + "/" + totalPage;
		}

		private var _totalPage:uint = 1;

		/**总共页*/
		public function get totalPage():uint
		{
			return _totalPage;
		}

		/**
		 * @private
		 */
		public function set totalPage(value:uint):void
		{
			_totalPage = value;
			pageLabel.text = currentPage + "/" + totalPage;
		}
		
		//--------------------------------------------------------------------------
		//
		//  public
		//
		//--------------------------------------------------------------------------

		//--------------------------------------------------------------------------
		//
		//  private
		//
		//--------------------------------------------------------------------------
		private function init():void
		{
			var tempFirstUp:Bitmap = UIManager.defaultSkin.getUIByName("page_newest_up") as Bitmap ;
			var tempFirstDown:Bitmap = UIManager.defaultSkin.getUIByName("page_newest_down") as Bitmap ;
			var tempFirstOver:Bitmap = UIManager.defaultSkin.getUIByName("page_newest_up") as Bitmap ;
			tempFirstOver.alpha = 0.4;
			firstPage = new SimpleButton(tempFirstUp,tempFirstOver,tempFirstDown);
			firstPage.hitTestState = tempFirstOver;
			firstPage.x = 0;
			firstPage.y = 0;
			firstPage.addEventListener(MouseEvent.CLICK,eventHandler);
			
			var temppreviousUp:Bitmap = UIManager.defaultSkin.getUIByName("page_newer_up") as Bitmap;
			var temppreviousDown:Bitmap = UIManager.defaultSkin.getUIByName("page_newer_down") as Bitmap;
			var temppreviousOver:Bitmap = UIManager.defaultSkin.getUIByName("page_newer_up") as Bitmap;
			temppreviousOver.alpha = 0.4;
			previousPage = new SimpleButton(temppreviousUp,temppreviousOver,temppreviousDown);
			previousPage.hitTestState = temppreviousOver;
			previousPage.x = firstPage.x + firstPage.width + 10;
			previousPage.y = 0;
			previousPage.addEventListener(MouseEvent.CLICK,eventHandler);
			
			pageLabel = new Label();
			pageLabel.selectable = false;
			pageLabel.text = currentPage + "/" + totalPage;
			pageLabel.x = previousPage.width + previousPage.x + 10;
			pageLabel.y = 0;
			
			var tempNextUp:Bitmap = UIManager.defaultSkin.getUIByName("page_older_up") as Bitmap;
			var tempNextDown:Bitmap = UIManager.defaultSkin.getUIByName("page_older_down") as Bitmap;
			var tempNextOver:Bitmap = UIManager.defaultSkin.getUIByName("page_older_up") as Bitmap;
			tempNextOver.alpha = 0.4;
			nextPage = new SimpleButton(tempNextUp,tempNextOver,tempNextDown);
			nextPage.hitTestState = tempNextOver;
			nextPage.x = pageLabel.x + pageLabel.width + 10;
			nextPage.y = 0;
			nextPage.addEventListener(MouseEvent.CLICK,eventHandler);
			
			var tempLastUp:Bitmap = UIManager.defaultSkin.getUIByName("page_oldest_up") as Bitmap;
			var tempLastDown:Bitmap = UIManager.defaultSkin.getUIByName("page_oldest_down") as Bitmap;
			var tempLastOver:Bitmap = UIManager.defaultSkin.getUIByName("page_oldest_up") as Bitmap;
			tempLastOver.alpha = 0.4;
			lastPage = new SimpleButton(tempLastUp,tempLastOver,tempLastDown);
			lastPage.hitTestState = tempFirstOver;
			lastPage.x = nextPage.width + nextPage.x + 10;
			lastPage.y = 0;
			lastPage.addEventListener(MouseEvent.CLICK,eventHandler);
			
			this.addChild(firstPage);
			this.addChild(previousPage);
			this.addChild(pageLabel);
			this.addChild(nextPage);
			this.addChild(lastPage);
		}
		//--------------------------------------------------------------------------
		//
		//  event
		//
		//--------------------------------------------------------------------------
		private function eventHandler(e:MouseEvent):void
		{
			if(totalPage == 1)
			{
				return;
			}
			switch(e.currentTarget)
			{
				case firstPage:
					if(currentPage != 1)
					{
						currentPage = 1;
						this.dispatchEvent(new Event(CLICK_FIRSTPAGE));
					}
					break;
				case lastPage:
					if(currentPage != totalPage)
					{
						currentPage = totalPage;
						this.dispatchEvent(new Event(CLICK_LASTPAGE));
					}
					break;
				case previousPage:
					if(currentPage != 1)
					{
						currentPage --;
						this.dispatchEvent(new Event(CLICK_PREVIOUSPAGE));
					}
					break;
				case nextPage:
					if(currentPage != totalPage)
					{
						currentPage ++;
						this.dispatchEvent(new Event(CLICK_NEXTPAGE));
					}
					break;
			}
		}
	}
}